﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ST.Common.Util.Mvc
{
    public partial class PageData<T>
    {
        /// <summary>
        /// 记录总数
        /// </summary>
        public int total { get; set; }

        /// <summary>
        /// 当前页码（从1开始）
        /// </summary>
        public int pnum { get; set; }

        /// <summary>
        /// 每页数量
        /// </summary>
        public int psize { get; set; }

        /// <summary>
        /// 计算合计的列
        /// </summary>
        public string[] sumcol { get; set; }

        /// <summary>
        /// 计算出的总合计
        /// </summary>
        public object[] sumtotal { get; set; }

        /// <summary>
        /// 当前页的合计
        /// </summary>
        public object[] sumpage { get; set; }

        /// <summary>
        /// 列表数据
        /// </summary>
        public IList<T> list { get; set; }

        public PageData()
        {
        }

        public PageData(IList<T> _list)
        {
            list = _list;
            psize = _list.Count;
            pnum = 1;
            total = psize;
        }

        public PageData(int _pnum, int _psize)
        {
            pnum = _pnum;
            psize = _psize;
        }

        public PageData(int _pnum, int _psize, string[] _sumcol)
        {
            pnum = _pnum;
            psize = _psize;
            sumcol = _sumcol;
        }

        public PageData(int _total, int _pnum, int _psize, IList<T> _list)
        {
            total = _total;
            pnum = _pnum;
            psize = _psize;
            list = _list;
        }

        public PageData(int _total, int _pnum, int _psize, IList<T> _list, string[] _sumcol, object[] _sumtotal)
        {
            total = _total;
            pnum = _pnum;
            psize = _psize;
            list = _list;
            sumcol = _sumcol;
            sumtotal = _sumtotal;
        }

        public PageData(int _total, int _pnum, int _psize, IList<T> _list, string[] _sumcol, object[] _sumtotal, object[] _sumpage)
        {
            total = _total;
            pnum = _pnum;
            psize = _psize;
            list = _list;
            sumcol = _sumcol;
            sumtotal = _sumtotal;
            sumpage = _sumpage;
        }

        /// <summary>
        /// 加载生成分页列表结果并赋值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="getOnlyTotalCount"></param>
        public void Load(IQueryable<T> query, bool getOnlyTotalCount = false)
        {
            total = query.Count();
            if (getOnlyTotalCount)
            {
                return;
            }
            if (pnum > 1)
            {
                query = query.Skip(psize * (pnum - 1));
            }
            if (psize > 0)
            {
                query = query.Take(psize);
            }
            list = query.ToList();
        }

        /// <summary>
        /// 加载生成分页列表结果，并赋值列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        public void LoadList(IQueryable<T> query)
        {
            if (pnum > 1)
            {
                query = query.Skip(psize * (pnum - 1));
            }
            if (psize > 0)
            {
                query = query.Take(psize);
            }
            list = query.ToList();
        }

        /// <summary>
        /// 加载生成分页列表结果并赋值总数，但不赋值结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        public IList<T> LoadQuery(IQueryable<T> query)
        {
            total = query.Count();
            if (pnum > 1)
            {
                query = query.Skip(psize * (pnum - 1));
            }
            if (psize > 0)
            {
                query = query.Take(psize);
            }
            var list = query.ToList();
            return list;
        }
    }

    public partial class PageData
    {
        /// <summary>
        /// 记录总数
        /// </summary>
        public int total { get; set; }

        /// <summary>
        /// 当前页码（从1开始）
        /// </summary>
        public int pnum { get; set; }

        /// <summary>
        /// 每页数量
        /// </summary>
        public int psize { get; set; }

        /// <summary>
        /// 计算合计的列
        /// </summary>
        public string[] sumcol { get; set; }

        /// <summary>
        /// 计算出的总合计
        /// </summary>
        public object[] sumtotal { get; set; }

        /// <summary>
        /// 当前页的合计
        /// </summary>
        public object[] sumpage { get; set; }

        /// <summary>
        /// 列表数据
        /// </summary>
        public object list { get; set; }

        public PageData()
        {
        }

        public PageData(int _pnum, int _psize)
        {
            pnum = _pnum;
            psize = _psize;
        }

        public PageData(int _total, int _pnum, int _psize, object _list)
        {
            total = _total;
            pnum = _pnum;
            psize = _psize;
            list = _list;
        }

        public PageData(int _total, int _pnum, int _psize, object _list, string[] _sumcol, object[] _sumtotal)
        {
            total = _total;
            pnum = _pnum;
            psize = _psize;
            list = _list;
            sumcol = _sumcol;
            sumtotal = _sumtotal;
        }

        public PageData(int _total, int _pnum, int _psize, object _list, string[] _sumcol, object[] _sumtotal, object[] _sumpage)
        {
            total = _total;
            pnum = _pnum;
            psize = _psize;
            list = _list;
            sumcol = _sumcol;
            sumtotal = _sumtotal;
            sumpage = _sumpage;
        }

        public void Load<T>(IList<T> _list)
        {
            list = _list;
            total = _list != null ? _list.Count : 0;
            pnum = 1;
            psize = total;
        }

        /// <summary>
        /// 加载生成分页列表结果并赋值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="getOnlyTotalCount"></param>
        public void Load<T>(IQueryable<T> query, bool getOnlyTotalCount = false)
        {
            PageData<T> pageData = new PageData<T>(pnum, psize, sumcol);
            pageData.Load(query, getOnlyTotalCount);
            list = pageData.list;
            total = pageData.total;
        }

        /// <summary>
        /// 加载生成分页列表结果并赋值，但不赋值总数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="getOnlyTotalCount"></param>
        public void LoadList<T>(IQueryable<T> query)
        {
            PageData<T> pageData = new PageData<T>(pnum, psize, sumcol);
            pageData.LoadList(query);
            list = pageData.list;
        }

        /// <summary>
        /// 加载生成分页列表结果并赋值总数，但不赋值结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        public IList<T> LoadQuery<T>(IQueryable<T> query)
        {
            PageData<T> pageData = new PageData<T>(pnum, psize, sumcol);
            var pageList = pageData.LoadQuery(query);
            total = pageData.total;
            return pageList;
        }
    }
}
